﻿using System;
using System.Collections.Generic;
using System.Windows.Input;
using Socium.Domain.Tests.Sociometric;
using Socium.Presentation;

namespace Socium.Tests.Procedures {
	class ElectionCriteriaLineItem : ViewModelBase {
		ElectionCriteria _election_criteria;

		#region Constructors

		public ElectionCriteriaLineItem(ElectionCriteria election_criteria) {
			_election_criteria = election_criteria;

			InitializeProperties();
			InitializeDeleteCommand();
		}

		public ElectionCriteriaLineItem() : this(null) {}

		static string GetDisplayName(ElectionCriteria election_criteria) {
			if (election_criteria == null) {
				return "New election criteria";
			}

			return election_criteria.ToString();
		}

		#endregion

		#region Editable properties

		public string Name { get; set; }
		public string Description { get; set; }
		public int MaxElections { get; set; }

		void InitializeProperties() {
			if (_election_criteria == null) {
				return;
			}

			Name = _election_criteria.Name;
			Description = _election_criteria.Description;
			MaxElections = _election_criteria.MaxElections;
		}

		#endregion

		#region Delete

		public ICommand DeleteCommand { get; private set; }
		public event Action<ElectionCriteriaLineItem> RequestDelete;

		void InitializeDeleteCommand() {
			DeleteCommand = new Command(OnRequestDelete);
		}

		void OnRequestDelete() {
			var handler = RequestDelete;
			if (handler != null) {
				handler(this);
			}
		}

		public void Delete(ICollection<ElectionCriteria> election_criterias) {
			if (_election_criteria != null) {
				election_criterias.Remove(_election_criteria);
			}
		}

		#endregion

		#region Save

		public bool CanSave() {
			if (string.IsNullOrEmpty(Name)) {
				return false;
			}

			return true;
		}

		public void Save(ICollection<ElectionCriteria> election_criterias) {
			var is_new = _election_criteria == null;
			if (is_new) {
				_election_criteria = new ElectionCriteria();
			}

			_election_criteria.Name = Name;
			_election_criteria.Description = Description;
			_election_criteria.MaxElections = MaxElections;

			//DisplayName = _election_criteria.ToString();
			if (is_new) {
				election_criterias.Add(_election_criteria);
			}
		}

		#endregion
	}
}
